/**
 * @Copyright (C) 2019 广州金鹏集团有限公司.
 * 本系统是商用软件,未经授权擅自复制或传播本程序的部分或全部将是非法的.
 * @创建人: 赵力
 * @创建时间: 2019-01-22 14:04
 * @版本: V1.0
 */
package com.ikcai.enginegraph.service.impl;

import com.ikcai.enginegraph.domain.base.BaseRelationship;
import com.ikcai.enginegraph.domain.node.Person;
import com.ikcai.enginegraph.domain.node.Weapon;
import com.ikcai.enginegraph.respository.PersonRepository;
import com.ikcai.enginegraph.respository.WeaponRepository;
import com.ikcai.enginegraph.service.IBaseRelationshipService;
import com.ikcai.enginegraph.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @类功能说明: 人物服务类实现类
 * @创建人: 赵力
 * @创建时间: 2019-01-22 14:04
 */
@Service
public class PersonServiceImpl implements IPersonService {

    @Autowired
    PersonRepository personRepository;

    @Autowired
    WeaponRepository weaponRepository;

    @Autowired
    IBaseRelationshipService relationshipService;

    @Override
    public Person addPerson(Person person) {
        return personRepository.save(person);
    }

    @Override
    public Person addPersonByName(String name) {
        Person person = new Person();
        person.setName(name);
        return personRepository.save(person);
    }

    @Override
    public Person getPersonByName(String name) {
        return personRepository.getPersonByName(name);
    }

    @Override
    public boolean deletePerson(Long id) {
        personRepository.deleteById(id);
        return true;
    }

    @Override
    public BaseRelationship addPersonRelationship(String name, String startName, String endName) {

        //获取人物节点
        Person startPerson = getPersonByName(startName);
        Person endPerson = getPersonByName(endName);

        /**
         * 添加关系有如下几种情况：
         * 1.人物节点全部存在:
         *   1.1关系存在
         *   1.2关系不存在
         * 2.开始人物节点存在，结束人物节点不存在
         * 3.开始人物节点不存在，结束人物节点存在
         * 4.人物节点均不存在
         */
        if(startPerson != null && endPerson != null) { // 1.人物节点全部存在
            BaseRelationship relationship = relationshipService.findPersonRelationshipByName(name,startName,endName);
            if(relationship != null) {
                return relationship;
            }
        }else if(startPerson != null && endPerson == null) { //2.开始人物节点存在，结束人物节点不存在
            endPerson = addPersonByName(endName);
        }else if(startPerson == null && endPerson != null) { //3.开始人物节点不存在，结束人物节点存在
            startPerson = addPersonByName(startName);
        }else if(startPerson == null && endPerson == null) { //4.人物节点均不存在
            startPerson = addPersonByName(startName);
            endPerson = addPersonByName(endName);
        }

        BaseRelationship relationship = relationshipService.addRelationship(name,startPerson,endPerson);
        //更新开始节点关系
        Set<BaseRelationship> relationships = startPerson.getRelationships();
        if(relationships == null) {
            relationships = new HashSet<>();
        }
        relationships.add(relationship);
        startPerson.setRelationships(relationships);
        personRepository.save(startPerson);
        return relationship;
    }

    @Override
    public BaseRelationship getPersonRelationship(String name, String startName, String endName) {
        return relationshipService.findPersonRelationshipByName(name,startName,endName);
    }

    @Override
    public BaseRelationship addPersonWeapon(String name, String startName, String endName) {

        Person startNode = getPersonByName(startName);
        Weapon endNode = weaponRepository.getWeaponByName(endName);

        /**
         * 添加关系有如下几种情况：
         * 1.节点全部存在:
         *   1.1关系存在
         *   1.2关系不存在
         * 2.开始节点存在，结束节点不存在
         * 3.开始节点不存在，结束节点存在
         * 4.节点均不存在
         */
        if(startNode != null && endNode != null) { // 1.节点全部存在
            BaseRelationship relationship = relationshipService.findPersonRelationshipByName(name,startName,endName);
            if(relationship != null) {
                return relationship;
            }
        }else if(startNode != null && endNode == null) { //2.开始节点存在，结束人物节点不存在
            Weapon weapon = new Weapon();
            weapon.setName(endName);
            endNode = weaponRepository.save(weapon);
        }else if(startNode == null && endNode != null) { //3.开始人物节点不存在，结束人物节点存在
            startNode = addPersonByName(startName);
        }else if(startNode == null && endNode == null) { //4.人物节点均不存在
            startNode = addPersonByName(startName);
            Weapon weapon = new Weapon();
            weapon.setName(endName);
            endNode = weaponRepository.save(weapon);
        }

        BaseRelationship relationship = relationshipService.addRelationship(name,startNode,endNode);
        //更新开始节点关系
        Set<BaseRelationship> relationships = startNode.getRelationships();
        if(relationships == null) {
            relationships = new HashSet<>();
        }
        relationships.add(relationship);
        startNode.setRelationships(relationships);
        personRepository.save(startNode);
        return relationship;
    }
}
